package jdbctest;

import pojo.Account;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * @author tongchen
 * @create 2022-11-03 16:55
 */
public class JdbcTest3 {
    public static void main(String[] args) throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        String url="jdbc:mysql://127.0.0.1:3306/exer?useSSL=false";
        String username="root";
        String password="abc123";
        Connection connection = DriverManager.getConnection(url, username, password);
        //3.定义sql语句
        String sql="select*from account";
        //4.创建sql对象
        Statement statement = connection.createStatement();
        //5.执行sql语句
        ResultSet resultSet = statement.executeQuery(sql);//(使用查询类型的语句)
        //6.处理返回结果
        //6.1创建集合
        ArrayList<Account> list = new ArrayList<>();
        while(resultSet.next()){
            //6.2创建对象
            Account account = new Account();
            int anInt = resultSet.getInt(1);
            String string = resultSet.getString(2);
            double aDouble = resultSet.getDouble(3);
            account.setId(anInt);
            account.setName(string);
            account.setMoney(aDouble);
            list.add(account);
        }
        //测试结果
        System.out.println(list);
        //7.关闭资源
        resultSet.close();
        statement.close();
        connection.close();


    }
}
